﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ZY.IBLL;
using ZY.Model;

namespace ZY.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        IBaseBLL bll;
        public UserController(IBaseBLL bll)
        {
            this.bll = bll;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> Add(UserModel t)
        {
            return await bll.Add(t);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="totalcount"></param>
        /// <param name="totalpage"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult All( int pageindex = 1, int pagesize = 2)
        {
            int totalcount = 0;
            int totalpage = 0;
            var list=bll.All(out totalcount, out totalpage,pageindex,pagesize);
            return Ok(new
            {
                totalcount = totalcount,
                totalpage = totalpage,
                list = list
            });
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<UserModel> Find(int id)
        {
            return await bll.Find(id);
        }
        /// <summary>
        /// 状态表显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IQueryable<ZTModel> Show()
        {
            return bll.Show();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> Update(UserModel t)
        {
            return await bll.Update(t);
        }
        [HttpPost]
        public IActionResult Img(IFormFile file)
        {
            string p = Directory.GetCurrentDirectory() + @"/wwwroot/Pic/" + file.FileName;
            using(FileStream fs=new FileStream(p,FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Close();
            }
            return Ok("https://localhost:7044/Pic/" + file.FileName);
        }
    }
}
